/* Change this if the SERVER_NAME environment variable does not report
	the true name of your web server. */
#if 1
#define SERVER_NAME cgiServerName
#endif
#if 0
#define SERVER_NAME "www.boutell.com"
#endif

/* You may need to change this, particularly under Windows;
	it is a reasonable guess as to an acceptable place to
	store a saved environment in order to test that feature. 
	If that feature is not important to you, you needn't
	concern yourself with this. */

#ifdef WIN32
#define SAVED_ENVIRONMENT "c:\\cgicsave.env"
#else
#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
#endif /* WIN32 */

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>


void FPGA_File();
void APP_File();

int cgiMain() {

	/* Send the content type, letting the browser know this is HTML */
	cgiHeaderContentType("text/html; charset=UTF-8");

	/* Top of the page */
	fprintf(cgiOut, "<HTML><HEAD>\n");
	fprintf(cgiOut, "<TITLE>系统文件升级</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY><H3>系统文件升级</H3>\n");

	/* If a submit button has already been clicked, act on the 
		submission of the form. */
	 system(" mount /dev/mmcblk1p1 /mnt/");

	if (cgiFormSubmitClicked("btn_fpga_upgrade") == cgiFormSuccess)
	{
//		system(" mount /dev/mmcblk1p1 /mnt/";)
		FPGA_File();
//printf("<a href=\"/index.html\">return index.html</a>");

	}
	if (cgiFormSubmitClicked("btn_app_upgrade") == cgiFormSuccess)
	{
		APP_File();
//printf("<a href=\"/index.html\">return index.html</a>");

	}

	 system("umount /mnt/");
	 system("sync");


	/* Now show the form */
	ShowForm();


	/* Finish up the page */
	fprintf(cgiOut, "</BODY></HTML>\n");
	return 0;
}


void FPGA_File()
{

	FILE *fp = NULL;

	cgiFilePtr file;
	char name[1024];
	char save_path[1024];
	char contentType[1024];
	char buffer[1024];
	int size;
	int got;
	int integer;
	int remainder;
	int count = 0;
	

	if (cgiFormFileName("fpga_file", name, sizeof(name)) != cgiFormSuccess) {
		printf("<p>没有FPGA文件.<p>\n");
		return;
	} 

	sprintf(save_path,"/mnt/%s",name);
	fp = fopen(save_path,"wb+");

	cgiFormFileSize("fpga_file", &size);

	count = 0;
	integer = size/1024;
	remainder = size%1024;

	cgiFormFileContentType("fpga_file", contentType, sizeof(contentType));
	if (cgiFormFileOpen("fpga_file", &file) != cgiFormSuccess) {
		fprintf(cgiOut, "Could not open the fpga_file.<p>\n");
		return;
	}
	while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
		cgiFormSuccess)
	{
		count++;
		if(count <= integer)
		{
			fwrite(buffer,1024,1,fp);
		}
		else
		{
			fwrite(buffer,remainder,1,fp);
		}
	}

	cgiFormFileClose(file);
	fclose(fp);

}
void APP_File()
{
	FILE *fp = NULL;

	cgiFilePtr file;
	char name[1024];
	char save_path[1024];
	char contentType[1024];
	char buffer[1024];
	int size;
	int got;
	int integer;
	int remainder;
	int count = 0;
	

	if (cgiFormFileName("app_file", name, sizeof(name)) != cgiFormSuccess) {
		printf("<p>没有APP文件.<p>\n");
		return;
	} 

	sprintf(save_path,"/mnt/%s",name);
	fp = fopen(save_path,"wb+");


	cgiFormFileSize("app_file", &size);
	
	count = 0;
	integer = size/1024;
	remainder = size%1024;

	cgiFormFileContentType("app_file", contentType, sizeof(contentType));

	if (cgiFormFileOpen("app_file", &file) != cgiFormSuccess) {
		fprintf(cgiOut, "Could not open the app_file.<p>\n");
		return;
	}

	while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
		cgiFormSuccess)
	{
		count++;
		if(count <= integer)
		{
			fwrite(buffer,1024,1,fp);
		}
		else
		{
			fwrite(buffer,remainder,1,fp);
		}
	}

	cgiFormFileClose(file);
	fclose(fp);
}
void ShowForm()
{
	fprintf(cgiOut, "<!-- 2.0: multipart/form-data is required for file uploads. -->");
	fprintf(cgiOut, "<form method=\"POST\" enctype=\"multipart/form-data\" ");
	fprintf(cgiOut, "	action=\"");
	cgiValueEscape(cgiScriptName);
	fprintf(cgiOut, "\">\n");
	fprintf(cgiOut, "<p>FPGA升级:\n");
	fprintf(cgiOut, "<input type=\"file\" name=\"fpga_file\" value=\"\">\n");
	fprintf(cgiOut, "<input type=\"submit\" name=\"btn_fpga_upgrade\" value=\"升级\">\n");

	fprintf(cgiOut, "<p>App升级:&ensp;&ensp;\n");
	fprintf(cgiOut, "<input type=\"file\" name=\"app_file\" value=\"\">\n");
	fprintf(cgiOut, "<input type=\"submit\" name=\"btn_app_upgrade\" value=\"升级\">\n");

	fprintf(cgiOut, "<p>\n");

	fprintf(cgiOut, "<a href=\"/index.html\">返回主页</a>");
	fprintf(cgiOut, "</form>\n");
}


